package com.lrf.freeshipping.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrf.freeshipping.bean.ClientProductBean;
import com.lrf.freeshipping.jdbc.SQLConstant;
import com.lrf.freeshipping.service.ProductOption;
import com.lrf.freeshipping.tool.Common;

@SuppressWarnings("serial")
@WebServlet("/search")
public class SearchProductServlet extends HttpServlet{
    
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = req.getParameter("name");
		int page = req.getParameter("page")==null?1:Integer.valueOf(req.getParameter("page"));
		int pageSize = 10;
		List<ClientProductBean> beans = 
				  ProductOption.packageForClientProduct(SQLConstant.queryProductByTitle,"%"+name+"%", page-1, pageSize);
		String result = Common.packageJson(beans);
		PrintWriter writer = resp.getWriter();
		writer.println(result);
		writer.flush();	
	}

}
